package com.leostadyn.stackfarm.timestatistics.view.activity.admin.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.model.bean.AddProjectBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.suke.widget.SwitchButton;

import java.util.HashMap;

public class ProjectAddActivity extends BaseActivity implements View.OnClickListener {
    TextView title;
    EditText project_name;
    EditText project_id;
    SwitchButton button;
    String[] states = {"1", "0"};
    String state = states[0];
    Button save_btn;
    HashMap<String, String> addmap = new HashMap<>();
    private int index = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_add);
        initView();
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            checkButton();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            checkButton();
        }
    };

    public void initView() {
        title = findViewById(R.id.nav_txt_title);
        title.setText("添加项目");
        findViewById(R.id.nav_img_back).setOnClickListener(v -> {
            finish();
        });

        project_name = findViewById(R.id.project_add_edt_project_name);
        project_id = findViewById(R.id.project_add_edt_project_id);

        project_name.addTextChangedListener(mTextWatcher);
        project_id.addTextChangedListener(mTextWatcher);

        button = findViewById(R.id.switch_button);
        button.setChecked(true);

        save_btn = findViewById(R.id.project_add_btn_confirm);
        save_btn.setOnClickListener(this);

        button.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton buttonView, boolean isChecked) {
                state = states[(isChecked ? 0 : 1)];
                String status = state.equals("0") ? "关" : "开";
                ToastUtil.show(ProjectAddActivity.this, status);
                checkButton();
            }
        });

    }

    public boolean isFill() {
        String staff_edit_id = project_name.getText().toString().trim();
        if (TextUtils.isEmpty(staff_edit_id)) {
            return false;
        }

        String staff_edit_user_name = project_id.getText().toString().trim();
        return !TextUtils.isEmpty(staff_edit_user_name);
    }


    private void checkButton() {

        if (isFill()) {
            save_btn.setEnabled(true);
        } else {
            save_btn.setEnabled(false);
        }

    }

    public void addProject() {
        addmap.put("name", project_name.getText().toString().trim());
        addmap.put("code", project_id.getText().toString().trim());
        addmap.put("status", state);
        final KProgressHUD dialog = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true).setAnimationSpeed(2).setDimAmount(0.5f);
        dialog.show();

        AdminApi.getInstance().addProject(addmap).subscribe(new OriginalSubscriber<AddProjectBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                //ToastUtil.show(StaffListActivity.this, e.getMsg());
                dialog.dismiss();
                ToastUtil.show(ProjectAddActivity.this, e.getMsg());
            }

            @Override
            public void onNext(AddProjectBean addProjectBean, String msg) {
                dialog.dismiss();
                ToastUtil.show(ProjectAddActivity.this, "添加成功");
                Intent intent = new Intent();
                intent.putExtra("success", 1);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }

        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.project_add_btn_confirm:
                addProject();
                break;
        }
    }

}
